app.controller('DataReportGraphCtrl', ['$rootScope', '$scope', 'httpLoad',function($rootScope, $scope, httpLoad) {
	$rootScope.moduleTitle = '数据纵览 > 数据报表';
	$rootScope.link = '/statics/css/graph.css';
	$scope.itemsByPage = 5;
	$scope.allData = {};$scope.vmwareData = {};$scope.openstackData = {};$scope.appData = {}
	var setValue = function (key,data) {
		switch($scope.vendorType){
			case 'VMWARE':
				$scope.vmwareData[key] = data;
				break;
			case 'OPENSTACK':
				$scope.openstackData[key] = data;
				break;
			case 'APP':
				$scope.appData[key] = data;
				break;
			default:
				$scope.allData[key] = data;
				break;
		}
	}
	var getPlatform = function (datas) {
		httpLoad.loadData({
			url: '/cloudVendor/list',
			method: 'POST',
			data: {
				page: 1,
				rows: 10000,
				params: JSON.stringify([{"param": {"type": $scope.vendorType}, "sign": "EQ"}]),
			},
			noParam: true,
			success: function (data) {
				if (data.success) {
					datas.platformData = data.data.rows;
				}
			}
		});
	};
	$scope.param = {
		page:1,
		rows:5
	};
	var getAbstractsData = function (param) {
		httpLoad.loadData({
			url: '/platform/abstracts',
			method: 'GET',
			data: param,
			success: function (data) {
				if (data.success) {
					setValue('overviewData',data.data);
				}
			}
		});
	};
	$scope.getHostList = function (page) {
		$scope.param.page = page || $scope.param.page;
		httpLoad.loadData({
			url: '/platform/hosts',
			method: 'POST',
			data: $scope.param,
			noParam: true,
			success: function (data) {
				if (data.success) {
					setValue('hostList',data.data.rows)
					$scope.totalCount = data.data.total;
					if (data.data.rows.length == 0) {
						$scope.isImageData = true;
					} else $scope.isImageData = false;
				} else {
					$scope.isImageData = true;
				}
			}
		});
	};
	$scope.loadVmData = function (row) {
		row.isShow = !row.isShow;
		if(row.vmList) return;
		httpLoad.loadData({
			url: '/vm/list',
			method: 'POST',
			data: {
				page:1,
				rows:10000,
				params:JSON.stringify([{"param": {"hostId": row.id}, "sign": "EQ"}])
			},
			noParam: true,
			success: function (data) {
				if (data.success) {
					row.vmList = data.data.rows;
					angular.forEach(row.vmList, function (data, index) {
						//状态
						data.isShowRecover = data.isRecover = data.isShowPaused = data.isPaused = data.isShowSuspended = data.isSuspended = data.isShowActive = data.isActive = data.isShowStart = data.isStart = data.isShowStop = data.isStop = false;
						switch (data.status) {
							case 'RUNNING':
								data.isShowStop = true;
								data.isStop = false;
								data.isShowRestart = true;
								data.isRestart = false;
								data.isShowStart = false;
								data.isShowPaused = true;
								data.isPaused = false;
								data.isShowSuspended = true;
								data.isSuspended = false;
								data.statusColor = 'success';
								break;
							case 'STOPPED':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = true;
								data.isStart = false;
								data.isShowRecover = true;
								data.isRecover = false;
								data.isShowActive = true;
								data.isActive = false;
								data.statusColor = 'warning';
								break;
							case 'STOPPING':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = true;
								data.isStart = true;
								data.isShowRecover = true;
								data.isRecover = true;
								data.isShowActive = true;
								data.isActive = true;
								data.statusColor = 'default';
								break;
							case 'STARTING':
								data.isShowStop = true;
								data.isStop = true;
								data.isShowRestart = true;
								data.isRestart = false;
								data.isShowStart = false;
								data.isShowPaused = true;
								data.isPaused = true;
								data.isShowSuspended = true;
								data.isSuspended = true;
								data.statusColor = 'default';
								break;
							case 'SUSPENDED':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = false;
								data.isShowActive = true;
								data.isActive = false;
								data.statusColor = 'primary';
								break;
							case 'SUSPENDING':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = false;
								data.isShowActive = true;
								data.isActive = true;
								data.statusColor = 'default';
								break;
							case 'ACTIVING':
								data.isShowStop = true;
								data.isStop = true;
								data.isShowRestart = true;
								data.isRestart = false;
								data.isShowStart = false;
								data.isShowPaused = true;
								data.isPaused = true;
								data.isShowSuspended = true;
								data.isSuspended = true;
								data.statusColor = 'default';
								break;
							case 'PAUSED':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = false;
								data.isShowRecover = true;
								data.isRecover = false;
								data.statusColor = 'warning';
								break;
							case 'PAUSING':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = false;
								data.isShowRecover = true;
								data.isRecover = true;
								data.statusColor = 'default';
								break;
							case 'RECOVERING':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = false;
								data.isShowPaused = true;
								data.isPaused = true;
								data.isShowSuspended = true;
								data.isSuspended = true;
								data.statusColor = 'default';
								break;
							case 'PAUSING':
								data.isShowStop = true;
								data.isStop = true;
								data.isShowRestart = true;
								data.isRestart = false;
								data.isShowStart = false;
								data.statusColor = 'default';
								break;
							case 'BUILDING':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = true;
								data.isStart = true;
								data.isShowRecover = true;
								data.isRecover = true;
								data.isShowActive = true;
								data.isActive = true;
								data.statusColor = 'default';
								break;
							case 'RESTARTING':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = false;
								data.isShowPaused = true;
								data.isPaused = true;
								data.isShowSuspended = true;
								data.isSuspended = true;
								data.statusColor = 'default';
								break;
							case 'EXPIRED':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = true;
								data.isStart = true;
								data.isShowRecover = true;
								data.isRecover = true;
								data.isShowActive = true;
								data.isActive = true;
								data.statusColor = 'default';
								break;
							case 'EXCEPTION':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = true;
								data.isStart = true;
								data.isShowRecover = true;
								data.isRecover = true;
								data.isShowActive = true;
								data.isActive = true;
								data.statusColor = 'danger';
								break;
							case 'SYNSEXCEPTION':
								data.isShowStop = false;
								data.isShowRestart = true;
								data.isRestart = true;
								data.isShowStart = true;
								data.isStart = true;
								data.isShowRecover = true;
								data.isRecover = true;
								data.isShowActive = true;
								data.isActive = true;
								data.statusColor = 'danger';
								break;
						}
					});
				}
			}
		});
	};
	$scope.alarmParam = {
		page:1,
		rows:5
	};
	$scope.loadAlarmData = function(page){
		$scope.alarmParam.page = page || $scope.alarmParam.page;
		httpLoad.loadData({
			url:'/platform/alarms',
			method: 'POST',
			data: $scope.alarmParam,
			noParam: true,
			success:function(data){
				if(data.success&&data.data.rows){
					setValue('alarmList',data.data.rows)
					$scope.alarmTotalCount = data.data.total;
					if(data.data.rows.length==0) {
						$scope.isAlarmData = true;
					} else $scope.isAlarmData = false;
				}else{
					$scope.isAlarmData = true;
				}
			}
		});
	};
	$scope.selectTab = function (type) {
		$scope.vendorType = type;
		var data;
		switch($scope.vendorType){
			case 'VMWARE':
				data = $scope.vmwareData;
				break;
			case 'OPENSTACK':
				data = $scope.openstackData;
				break;
			case 'APP':
				data = $scope.appData;
				break;
			default:
				data = $scope.allData;
				break;
		}
		if(!data.platformData) getPlatform(data);//平台实例数据不存在时获取值
		$scope.search(data);
	};
	//搜索
	//对参数进行处理，去除空参数
	var toObjFormat = function (obj) {
		for (var a in obj) {
			if (obj[a] == "") delete obj[a];
		}
		return obj;
	};
	$scope.search = function (data) {
		var params = [];
		var param1 = toObjFormat({
			vendorId:data.vendorId
		});
		if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'EQ'});
		$scope.param = {
			page: 1,
			rows: 5,
			type:	$scope.vendorType,
			params: angular.toJson(params)
		};
		$scope.alarmParam = {
			page: 1,
			rows: 5,
			type:	$scope.vendorType,
			params: angular.toJson(params)
		};
		var param = toObjFormat({
			type:$scope.vendorType,
			vendorId:data.vendorId
		});
		getAbstractsData(param);
		$scope.getHostList();
		$scope.loadAlarmData();
	};
	$scope.export = function (data) {
		var param = toObjFormat({
			type:$scope.vendorType,
			vendorId:data.vendorId
		});
		var params = JSON.stringify(param);
		location.href = '/platform/export?params='+ params;
	}
}]);